package thinkingInJava.others;

//http://www.tuicool.com/articles/qUJFbi
//协变返回的用途
public class CovariantReturn {

	public static void main(String[] args) {
		printDivider("person1");
		Person person1 = getPerson1();
		System.out.println("person1的名字是" + person1.getName());

		printDivider("person");
		Person person = getPerson();
		System.out.println("person的名字是" + person.getName());
		SuperMan superMan = (SuperMan) person;
		System.out.println("通过强转获取到的超人高度是这样的" + superMan.getHeight());

		// ////这里不能获取高度，会报空的，因为父类没有高度属性
		printDivider("man");
		SuperMan man = getSuperMan();
		System.out.println("man的名字是" + man.getName());

		printDivider("man1");
		SuperMan man1 = getSuperMan1();
		System.out.println("man1的名字是" + man1.getName() + ",man1的高度是"+ man1.getHeight());
	}

	public static Person getPerson() {
		SuperMan man = new SuperMan();
		man.setHeight(100);
		man.setName("大明");
		return man;
	}

	public static SuperMan getSuperMan() {
		Person person = new SuperMan();
		person.setName("小虎");
		return (SuperMan) person;
	}

	public static Person getPerson1() {
		Person person = new Person();
		person.setName("无语");
		return person;
	}

	public static SuperMan getSuperMan1() {
		SuperMan man = new SuperMan();
		man.setName("不知道");
		man.setHeight(120);
		return man;
	}

	private static void printDivider(String str) {
		System.out.println("*********************" + str + "**********************");
	}
}

class Person {
	/** 假设每个人都有一个名字 **/
	String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}// person

class SuperMan extends Person {
	/** 假设每个超人都有身高 **/
	int height;

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
}// SuperMan
